{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Optimizer\n",
    "\n",
    "## 介绍\n",
    "Optimizer是optimizer模块中的一个核心类，提供了因子算法参数优化的功能\n",
    "\n",
    "*** 步骤 ***\n",
    "1. 实例化Optimizer\n",
    "2. 进行因子计算和参数优化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# step 1 实例化Optimizer\n",
    "\n",
    "## __init__\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.__init__(*args, **kwargs) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 初始化Optimizer\n",
    "\n",
    "**参数：**\n",
    "\n",
    "\n",
    "|参数名|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|dataview|是 |jaqs.data.Dataview|包含了因子相关的所有标的证券及因子计算所要用到的所有字段的数据集|\n",
    "|formula|否 |string|需要优化的公式：如'(open - Delay(close, LEN1)) / Delay(close, LEN2)'|\n",
    "|params|否 |dict|需要优化的参数范围：如{\"LEN1\"：range(1,10,1),\"LEN2\":range(1,10,1)}|\n",
    "|name|否 |string|信号的名称|\n",
    "|price |是，price与daily_ret二选一  |pandas.DataFrame|因子涉及到的股票的价格数据，用于作为进出场价用于计算收益,日期为索引，股票品种为columns|\n",
    "|daily_ret |是，price与daily_ret二选一  |pandas.DataFrame| 因子涉及到的股票的每日收益，日期为索引，股票品种为columns|\n",
    "|benchmark_price | 否，benchmark_price与daily_benchmark_ret二选一  |pandas.DataFrame or pandas.Series|基准价格，日期为索引。用于计算因子涉及到的股票的持有期**相对收益**--相对基准。默认为空，为空时计算的收益为**绝对收益**。|\n",
    "|daily_benchmark_ret | 否，benchmark_price与daily_benchmark_ret二选一  |pandas.DataFrame or pandas.Series|基准日收益，日期为索引。用于计算因子涉及到的股票的持有期**相对收益**--相对基准。默认为空，为空时计算的收益为**绝对收益**。|\n",
    "|high |否  |pandas.DataFrame|因子涉及到的股票的最高价数据,用于计算持有期潜在最大上涨收益,日期为索引，股票品种为columns,默认为空|\n",
    "|low |否  |pandas.DataFrame|因子涉及到的股票的最低价数据,用于计算持有期潜在最大下跌收益,日期为索引，股票品种为columns,默认为空|\n",
    "|period |否  |int|持有周期,默认为5,即持有5天|\n",
    "|n_quantiles |否  |int|根据每日因子值的大小分成n_quantiles组,默认为5,即将因子每天分成5组|\n",
    "|mask |否  |pandas.DataFrame|一张由bool值组成的表格,日期为索引，股票品种为columns，表示在做因子分析时是否要对某期的某个品种过滤。对应位置为True则**过滤**（剔除）——不纳入因子分析考虑。默认为空，不执行过滤操作|\n",
    "|can_enter |否  |pandas.DataFrame|一张由bool值组成的表格,日期为索引，股票品种为columns，表示某期的某个品种是否可以买入(进场)。对应位置为True则可以买入。默认为空，任何时间任何品种均可买入|\n",
    "|can_exit |否  |pandas.DataFrame|一张由bool值组成的表格,日期为索引，股票品种为columns，表示某期的某个品种是否可以卖出(出场)。对应位置为True则可以卖出。默认为空，任何时间任何品种均可卖出|\n",
    "|forward |否  |bool|收益对齐方式,forward=True则在当期因子下对齐下一期实现的收益；forward=False则在当期实现收益下对齐上一期的因子值。默认为True|\n",
    "|commission |否 |float|手续费比例,每次换仓收取的手续费百分比,默认为万分之八0.0008|\n",
    "|is_event |否 |bool|是否是事件(0/1因子),默认为否|\n",
    "|is_quarterly |否 |bool|是否是季度因子,默认为否|\n",
    "|register_funcs |否 |dict of function|待优化公式中用到的自定义方法所组成的dict,如{\"name1\":func1，\"name2\":func2}|\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataview loaded successfully.\n"
     ]
    }
   ],
   "source": [
    "from jaqs_fxdayu.research import Optimizer\n",
    "from jaqs_fxdayu.data import DataView\n",
    "\n",
    "# 加载dataview数据集\n",
    "dv = DataView()\n",
    "dataview_folder = './data'\n",
    "dv.load_dataview(dataview_folder)\n",
    "\n",
    "def _cut_negative(df):\n",
    "    return df[df>=0]\n",
    "\n",
    "# step 1：实例化Optimizer\n",
    "optimizer = Optimizer(dataview=dv,\n",
    "                      formula='CAL(- Correlation(vwap_adj, high, LEN))',\n",
    "                      params={\"CAL\":[\"Cut_Neg\",\"\"],\"LEN\":range(2,5,1),},\n",
    "                      name='test',\n",
    "                      price = dv.get_ts('close_adj'),\n",
    "                      high=dv.get_ts('high_adj'),\n",
    "                      low=dv.get_ts('low_adj'),\n",
    "                      benchmark_price=None,#=None求绝对收益 #=price_bench求相对收益\n",
    "                      period=30,\n",
    "                      n_quantiles=5,\n",
    "                      commission=0.0008,#手续费 默认0.0008\n",
    "                      is_event=False,#是否是事件(0/1因子)\n",
    "                      is_quarterly=False,\n",
    "                      register_funcs={\"Cut_Neg\":_cut_negative})#是否是季度因子 默认为False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# step 2 进行因子计算和参数优化\n",
    "\n",
    "## dataview\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.dataview `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 优化器用到的数据集\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<jaqs_fxdayu.data.dataview.DataView at 0x7fef58116e10>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "optimizer.dataview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## formula\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.formula `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 优化器所优化的因子表达式\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'CAL(- Correlation(vwap_adj, high, LEN))'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "optimizer.formula"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## params\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.params `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 优化器所优化的参数范围\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CAL': ['Cut_Neg', ''], 'LEN': range(2, 5)}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "optimizer.params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## name\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.name `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 优化器所优化的信号名称\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "optimizer.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## period\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.period `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 待优化因子所指定的调仓周期\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "optimizer.period"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## all_signals\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.all_signals `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 不同参数下计算得到的signal_data(关于signal_data的定义,详见文档digger部分-signal_data)所组成的字典\n",
    "- 在初始化Optimizer实例时指定了formula和params后，可以通过Optimizer.get_all_signals()计算不同参数下该公式算得的所有因子值；也可以手动指定\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(optimizer.all_signals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get_all_signals\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.get_all_signals() `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 通过Optimizer.get_all_signals()计算不同参数下该公式算得的所有因子值，并更新Optimizer.all_signals属性\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 13%\n",
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 5%\n",
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 2%\n",
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 92%\n",
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 94%\n",
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 94%\n"
     ]
    }
   ],
   "source": [
    "optimizer.get_all_signals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys([\"test{'CAL': 'Cut_Neg', 'LEN': 2}\", \"test{'CAL': 'Cut_Neg', 'LEN': 3}\", \"test{'CAL': 'Cut_Neg', 'LEN': 4}\", \"test{'CAL': '', 'LEN': 2}\", \"test{'CAL': '', 'LEN': 3}\", \"test{'CAL': '', 'LEN': 4}\"])\n",
      "                      signal    return  upside_ret  downside_ret  quantile\n",
      "trade_date symbol                                                         \n",
      "20170503   000001.SZ    -1.0  0.011546    0.031748     -0.038959         2\n",
      "           000002.SZ    -1.0  0.109486    0.165690     -0.021479         2\n",
      "           000008.SZ    -1.0 -0.071442   -0.005851     -0.119487         3\n",
      "           000009.SZ    -1.0 -0.089585   -0.003136     -0.165520         2\n",
      "           000027.SZ    -1.0 -0.016835    0.051678     -0.060567         5\n"
     ]
    }
   ],
   "source": [
    "print(optimizer.all_signals.keys())\n",
    "print(optimizer.all_signals[\"test{'CAL': '', 'LEN': 2}\"].head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## all_signals_perf\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.all_signals_perf `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 不同参数下计算得到的signal_data(关于signal_data的定义,详见文档digger部分-signal_data)的绩效表现所组成的字典\n",
    "- 在Optimizer.all_signals不为空的情况下，可以通过Optimizer.get_all_signals_perf()计算Optimizer.all_signals中不同因子的表现；\n",
    "- 在执行过Optimizer.get_all_signals_perf()后才能获取\n",
    "\n",
    "**返回:**\n",
    "\n",
    "dict of performance - 不同因子表现所组成的字典\n",
    "其中每个performance（因子表现）也是一个字典，由ic、ret、space三个key构成，分别对应ic分析表、收益分析表、潜在收益空间分析表(关于这三张表的说明，详见文档-analysis)\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(optimizer.all_signals_perf)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get_all_signals_perf\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.get_all_signals_perf(in_sample_range=None) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 在Optimizer.all_signals不为空的情况下，通过Optimizer.get_all_signals_perf()计算Optimizer.all_signals中不同因子的表现,并更新Optimizer.all_signals_perf属性；\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|in_sample_range |否|list of int|因子表现计算的时间范围,如[20140101,20160101] 表示计算因子表现所涵盖的数据范围只在20140101到20160101之间。默认为None,在全样本上计算因子表现|\n",
    "\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "optimizer.get_all_signals_perf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys([\"test{'CAL': 'Cut_Neg', 'LEN': 2}\", \"test{'CAL': 'Cut_Neg', 'LEN': 3}\", \"test{'CAL': 'Cut_Neg', 'LEN': 4}\", \"test{'CAL': '', 'LEN': 2}\", \"test{'CAL': '', 'LEN': 3}\", \"test{'CAL': '', 'LEN': 4}\"])\n",
      "dict_keys(['ic', 'ret', 'space', 'signal_name'])\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>return_ic</th>\n",
       "      <th>upside_ret_ic</th>\n",
       "      <th>downside_ret_ic</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>IC Mean</th>\n",
       "      <td>-0.025674</td>\n",
       "      <td>-0.038612</td>\n",
       "      <td>0.001965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Std.</th>\n",
       "      <td>0.058703</td>\n",
       "      <td>0.063177</td>\n",
       "      <td>0.058535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t-stat(IC)</th>\n",
       "      <td>-3.812846</td>\n",
       "      <td>-5.328101</td>\n",
       "      <td>0.292652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p-value(IC)</th>\n",
       "      <td>0.000280</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>0.770596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Skew</th>\n",
       "      <td>0.625732</td>\n",
       "      <td>0.689323</td>\n",
       "      <td>0.226355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IC Kurtosis</th>\n",
       "      <td>0.434047</td>\n",
       "      <td>0.495804</td>\n",
       "      <td>0.149208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. IR</th>\n",
       "      <td>-0.437363</td>\n",
       "      <td>-0.611175</td>\n",
       "      <td>0.033570</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             return_ic  upside_ret_ic  downside_ret_ic\n",
       "IC Mean      -0.025674      -0.038612         0.001965\n",
       "IC Std.       0.058703       0.063177         0.058535\n",
       "t-stat(IC)   -3.812846      -5.328101         0.292652\n",
       "p-value(IC)   0.000280       0.000001         0.770596\n",
       "IC Skew       0.625732       0.689323         0.226355\n",
       "IC Kurtosis   0.434047       0.495804         0.149208\n",
       "Ann. IR      -0.437363      -0.611175         0.033570"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(optimizer.all_signals_perf.keys())\n",
    "print(optimizer.all_signals_perf[\"test{'CAL': '', 'LEN': 2}\"].keys())\n",
    "optimizer.all_signals_perf[\"test{'CAL': '', 'LEN': 2}\"][\"ic\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## enumerate_optimizer\n",
    "\n",
    "- ` jaqs_fxdayu.research.signaldigger.optimizer.Optimizer.enumerate_optimizer(target_type=\"long_ret\",target=\"Ann. IR\",ascending=False,in_sample_range=None) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 枚举优化。按照指定的参数优化范围遍历每一种可能性，并给出最佳绩效下的排序结果\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|target_type |是|string|待优化的目标类型，有ic类、持有收益类、收益空间类三个大类，下分小类，具体类型见下|\n",
    "|target |是|string|待优化的目标绩效指标，有ic类、持有收益类、收益空间类三个大类，下分小类，具体类型见下|\n",
    "|ascending |否|bool|输出结果是否升序排列，默认为False--降序排列(指标越大排名越前)|\n",
    "|in_sample_range |否|list of int|样本内优化范围 默认为None,在全样本上优化|\n",
    "\n",
    "#### 优化目标的详细介绍\n",
    "目前，所有可优化的目标均围绕analysis模块中提供的三张绩效表——ic分析表、收益分析表、潜在收益空间分析表(关于这三张表的详细定义，参考文档-analysis)。\n",
    "\n",
    "#### target_type:\n",
    "* ic类:\n",
    "  return_ic/upside_ret_ic/downside_ret_ic\n",
    "* 持有收益类\n",
    "  long_ret/short_ret/long_short_ret/top_quantile_ret/bottom_quantile_ret/tmb_ret\n",
    "* 收益空间类\n",
    "  long_space/short_space/long_short_space/top_quantile_space/bottom_quantile_space/tmb_space\n",
    "\n",
    "#### target:\n",
    "* ic类 \n",
    "  \"IC Mean\", \"IC Std.\", \"t-stat(IC)\", \"p-value(IC)\", \"IC Skew\", \"IC Kurtosis\", \"Ann. IR\"\n",
    "* 持有收益类 \n",
    "  't-stat', \"p-value\", \"skewness\", \"kurtosis\", \"Ann. Ret\", \"Ann. Vol\", \"Ann. IR\", \"occurance\"\n",
    "* 收益空间类 \n",
    "  'Up_sp Mean','Up_sp Std','Up_sp IR','Up_sp Pct5', 'Up_sp Pct25 ','Up_sp Pct50 ', 'Up_sp Pct75','Up_sp Pct95','Up_sp Occur','Down_sp Mean','Down_sp Std', 'Down_sp IR', 'Down_sp Pct5','Down_sp Pct25 ','Down_sp Pct50 ','Down_sp Pct75', 'Down_sp Pct95','Down_sp Occur'\n",
    "  \n",
    "  \n",
    "**返回:**\n",
    "\n",
    "list of performance - 绩效的排序结果（只计算了样本内的绩效）\n",
    "其中每个performance（因子表现）是一个字典，由ic、ret、space三个key构成，分别对应ic分析表、收益分析表、潜在收益空间分析表(关于这三张表的说明，详见文档-analysis)\n",
    "\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ret_best = optimizer.enumerate_optimizer(target_type=\"top_quantile_ret\",#优化目标类型 \n",
    "                                         target=\"Ann. IR\",#优化目标     \n",
    "                                         in_sample_range=[20170501,20170801],#样本内范围\n",
    "                                         ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "dict_keys(['ic', 'ret', 'space', 'signal_name'])\n",
      "test{'CAL': 'Cut_Neg', 'LEN': 4}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>long_ret</th>\n",
       "      <th>long_short_ret</th>\n",
       "      <th>top_quantile_ret</th>\n",
       "      <th>bottom_quantile_ret</th>\n",
       "      <th>tmb_ret</th>\n",
       "      <th>all_sample_ret</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>t-stat</th>\n",
       "      <td>6.483452</td>\n",
       "      <td>-0.101407</td>\n",
       "      <td>5.392292</td>\n",
       "      <td>4.371209</td>\n",
       "      <td>-0.589115</td>\n",
       "      <td>9.697444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p-value</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.919580</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000030</td>\n",
       "      <td>0.557960</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>skewness</th>\n",
       "      <td>0.293834</td>\n",
       "      <td>-0.223391</td>\n",
       "      <td>0.571958</td>\n",
       "      <td>0.789755</td>\n",
       "      <td>0.139580</td>\n",
       "      <td>0.937413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>kurtosis</th>\n",
       "      <td>-0.358258</td>\n",
       "      <td>0.081883</td>\n",
       "      <td>0.368376</td>\n",
       "      <td>1.227729</td>\n",
       "      <td>0.237143</td>\n",
       "      <td>2.696370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. Ret</th>\n",
       "      <td>0.301933</td>\n",
       "      <td>-0.004470</td>\n",
       "      <td>0.440432</td>\n",
       "      <td>0.314685</td>\n",
       "      <td>-0.053747</td>\n",
       "      <td>0.329067</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. Vol</th>\n",
       "      <td>0.128063</td>\n",
       "      <td>0.118190</td>\n",
       "      <td>0.209361</td>\n",
       "      <td>0.253471</td>\n",
       "      <td>0.250885</td>\n",
       "      <td>0.238054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ann. IR</th>\n",
       "      <td>2.357699</td>\n",
       "      <td>-0.037818</td>\n",
       "      <td>2.103694</td>\n",
       "      <td>1.241505</td>\n",
       "      <td>-0.214231</td>\n",
       "      <td>1.382322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>occurance</th>\n",
       "      <td>62.000000</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>54.000000</td>\n",
       "      <td>101.000000</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>398.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            long_ret  long_short_ret  top_quantile_ret  bottom_quantile_ret  \\\n",
       "t-stat      6.483452       -0.101407          5.392292             4.371209   \n",
       "p-value     0.000000        0.919580          0.000000             0.000030   \n",
       "skewness    0.293834       -0.223391          0.571958             0.789755   \n",
       "kurtosis   -0.358258        0.081883          0.368376             1.227729   \n",
       "Ann. Ret    0.301933       -0.004470          0.440432             0.314685   \n",
       "Ann. Vol    0.128063        0.118190          0.209361             0.253471   \n",
       "Ann. IR     2.357699       -0.037818          2.103694             1.241505   \n",
       "occurance  62.000000       59.000000         54.000000           101.000000   \n",
       "\n",
       "             tmb_ret  all_sample_ret  \n",
       "t-stat     -0.589115        9.697444  \n",
       "p-value     0.557960        0.000000  \n",
       "skewness    0.139580        0.937413  \n",
       "kurtosis    0.237143        2.696370  \n",
       "Ann. Ret   -0.053747        0.329067  \n",
       "Ann. Vol    0.250885        0.238054  \n",
       "Ann. IR    -0.214231        1.382322  \n",
       "occurance  62.000000      398.000000  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(len(ret_best))\n",
    "print(ret_best[0].keys())\n",
    "print(ret_best[0][\"signal_name\"])\n",
    "ret_best[0][\"ret\"]"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:IIA]",
   "language": "python",
   "name": "conda-env-IIA-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
